package Change;

import EncapsulatingObjects.User;
import LoginSystem.LoginPage;
import PersonalHomepage.AdminHomepage;
import PersonalHomepage.UserHomepage;
import Util.Captcha;
import Util.Inspectionrules;
import Util.UiUtil;
import Util.UserDatabaseUtil;

import javax.swing.*;
import javax.swing.border.MatteBorder;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class ChangePassword {
    boolean statue;
    JFrame jf;
    JLabel tel;//电话号码
    JLabel password1;//旧密码
    JLabel password2;//新密码
    JLabel code;//验证码文本
    Captcha captcha;//显示数字验证码

    JTextField ttel;//电话号码
    JPasswordField tpass1;//旧密码
    JPasswordField tpass2;//新密码
    JTextField tverificationCode;//验证码输入框

    JButton OK;//确认
    JButton Cancel;//取消

    UserDatabaseUtil du;
    Connection conn;
    Statement stmt;
    ResultSet rs;
    UiUtil ui = new UiUtil();

    public ChangePassword() {
        try {
            init();
        } catch (Exception e) {
        }
        ui.setFrameCenter(jf);
        ui.setFrameImage(jf);
        ui.setLogo(jf);
        jf.setVisible(true);
    }

    private void init() throws SQLException, ClassNotFoundException {
        jf = new JFrame("ChangePassword");
        jf.setLayout(null);
        MatteBorder border = new MatteBorder(0, 0, 2, 0, new Color(17, 191, 191, 255));

        tel = new JLabel("TelNumber");
        password1 = new JLabel("OldPass");
        password2 = new JLabel("NewPass");

        ttel = new JTextField(20);
        ttel.setBackground(new Color(255, 229, 138, 255));
        ttel.setBorder(border);
        tpass1 = new JPasswordField(20);
        tpass1.setBackground(new Color(255, 229, 138, 255));
        tpass1.setBorder(border);
        tpass2 = new JPasswordField(20);
        tpass2.setBackground(new Color(255, 229, 138, 255));
        tpass2.setBorder(border);
        tpass2.addKeyListener(new KeyAdapter() {
            @Override
            public void keyTyped(KeyEvent e) {
                if (e.getKeyChar() == '\n') {
                    OKClicked();
                }
            }
        });
        captcha = new Captcha();

        code = new JLabel("Captcha");
        tverificationCode = new JTextField(20);
        tverificationCode.setBackground(new Color(255, 229, 138, 255));
        tverificationCode.setPreferredSize(new Dimension(150, 28));
        tverificationCode.setBorder(border);

        OK = new JButton("OK");
        Cancel = new JButton("Cancel");
        OK.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        Cancel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        jf.setSize(500, 400);

        tel.setBounds(40, 90, 70, 20);
        password1.setBounds(40, 130, 70, 20);
        password2.setBounds(40, 170, 70, 20);
        code.setBounds(40, 215, 70, 20);
        captcha.setBounds(285, 210, 70, 40);
        ttel.setBounds(120, 90, 300, 30);
        tpass1.setBounds(120, 130, 300, 30);
        tpass2.setBounds(120, 170, 300, 30);
        tverificationCode.setBounds(120, 210, 120, 30);
        OK.setBounds(110, 270, 100, 30);
        Cancel.setBounds(280, 270, 100, 30);

        OK.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                OKClicked();
            }
        });
        Cancel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                jf.dispose();
            }
        });

        jf.add(tel);
        jf.add(password1);
        jf.add(password2);

        jf.add(ttel);
        jf.add(tpass1);
        jf.add(tpass2);
        jf.add(code);
        jf.add(captcha);
        jf.add(tverificationCode);
        jf.add(OK);
        jf.add(Cancel);

        du = new UserDatabaseUtil();
        conn = du.getConnection();
        stmt = conn.createStatement();
        rs = stmt.executeQuery("select * from userdata");
        while (rs.next()) {
            if (rs.getBoolean("isLogin") == true) {
                if (rs.getBoolean("isAdmin"))
                    statue = true;
                else
                    statue = false;
            }
        }
    }

    private void OKClicked() {
        try {
            User user = new User();
            UserDatabaseUtil du = new UserDatabaseUtil();
            user.setPhone(ttel.getText().trim());
            String userPassword = String.valueOf(tpass1.getPassword()).trim();
            user.setPassword(String.valueOf(tpass2.getPassword()).trim());
            String vc = tverificationCode.getText().trim();//获取输入框验证码
            if (user.getPhone().equals("") || userPassword.equals("") || user.getPassword().equals("")) {
                JOptionPane.showMessageDialog(jf, "Please enter all information");
                captcha.nextCode();
                return;
            }
            if (!vc.equals(captcha.getCode())) {
                JOptionPane.showMessageDialog(jf, "Please enter the right verification code");
                captcha.nextCode();
                return;
            }
            if (!Inspectionrules.checkPass(jf, userPassword)) {
                return;
            }

            Connection conn = du.getConnection();
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery("select * from userdata");
            while (rs.next()) {
                if (rs.getBoolean("isLogin") == true) {
                    if (user.getPhone().equals(rs.getString("tel")) && userPassword.equals(rs.getString("password"))) {
                        if (!Inspectionrules.checkPass(jf, user.getPassword()) ) {
                            return;
                        }
                        if ((JOptionPane.showConfirmDialog
                                (jf, "Are you sure you want to change the password to " + user.getPassword()))
                                == JOptionPane.YES_OPTION) {
                            user.setAccount(rs.getString("account"));
                            du.updatePassword(conn, user);
                            JOptionPane.showMessageDialog(jf, "Password changed successfully！Please log in again!");
                            jf.dispose();
                            if(statue)
                                AdminHomepage.closePage();
                            else
                                UserHomepage.closePage();
                            stmt.close();
                            new LoginPage();
                            return;
                        }
                    } else {
                        JOptionPane.showMessageDialog(jf, "Password changed failed！Please enter right information.");
                        captcha.nextCode();
                    }
                }
            }
            stmt.close();
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }
}
